package com.passive.lock.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.passive.lock.model.vo.RightVo;
import com.passive.lock.model.vo.TRoleVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.user.RoleService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("ro")
public class RoleController {
	@Autowired
	private RoleService roleService;

	/**
	 * 角色信息管理
	 *
	 * @param model
	 * @returns
	 */

	@RequestMapping("/rolelist")
	@ResponseBody
	public Result roleList(@RequestParam(required = false) String groupid, HttpServletRequest req,
			HttpServletResponse resp) {

		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		if (groupid == null || "".equals(groupid)) {
			groupid = tUserVo.getGroupid();
		}
		try {
			Result result = roleService.roleList(groupid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/addrole")
	@ResponseBody
	public Result addRole(@RequestBody TRoleVo tRoleVo) {
		try {
			Result result = roleService.addRole(tRoleVo);

			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updaterole")
	@ResponseBody
	public Result updateRole(@RequestBody TRoleVo tRoleVo) {
		try {
			Result result = roleService.updateRole(tRoleVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleterole")
	@ResponseBody
	public Result deleteRole(@RequestBody TRoleVo tRoleVo) {
		try {
			Result result = roleService.deleteRole(tRoleVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	/**
	 * 权限信息管理
	 *
	 * @param model
	 * @returns
	 */
	@RequestMapping("/rightlist")
	@ResponseBody
	public Result rightList(@RequestParam Integer roleId, HttpServletRequest req,
			HttpServletResponse resp) {
		Result result = roleService.rightList(roleId);

		return result;
	}
	
	@RequestMapping("/addright")
	@ResponseBody
	public Result addRight(@RequestBody RightVo rightVo,HttpServletRequest req,
			HttpServletResponse resp) {
		
		return roleService.addRight(rightVo.getRoleId(),rightVo.getMenuIds());
	}
}
